﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Heyo.Pages;

namespace Heyo.Controls
{
    public class Modal: Decorator
    {
        public static readonly DependencyProperty ShowProperty = DependencyProperty.Register(
            "Show", typeof(bool), typeof(Modal), new PropertyMetadata(default(bool),ShowPropertyChanged));

        private static void ShowPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(!(bool)e.OldValue&&(bool)e.NewValue)
                ((Modal)d).ShowDialog();
            else if((bool)e.OldValue&& !(bool)e.NewValue)
                ((Modal)d).CloseDialog();
        }

        public static readonly DependencyProperty ShowCloseButtonProperty = DependencyProperty.Register(
            "ShowCloseButton", typeof(bool), typeof(Modal), new PropertyMetadata(true));

        public bool ShowCloseButton
        {
            get => (bool) GetValue(ShowCloseButtonProperty);
            set => SetValue(ShowCloseButtonProperty, value);
        }

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
            "Title", typeof(string), typeof(Modal), new PropertyMetadata(default(string)));

        public override UIElement Child { get; set; }

        public string Title
        {
            get => (string) GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }
        private DialogWindow window;
        private void ShowDialog()
        {
            
            window=new DialogWindow(new ModalDialogPage(this.DataContext, Title, Child,ShowCloseButton));
            window.Show();
        }

        private void CloseDialog()
        {
            window.Close();
        }

        public bool Show
        {
            get => (bool) GetValue(ShowProperty);
            set => SetValue(ShowProperty, value);
        }
    }
}
